package com.imddy.spbatch001.utils;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class TimeUtils {
    /**
     * 获取默认时间格式: yyyy-MM-dd HH:mm:ss
     */
    private static final DateTimeFormatter DEFAULT_DATETIME_FORMATTER = TimeFormat.LONG_DATETIME_PATTERN_LINE.formatter;

    private TimeUtils() {
        // no construct function
    }

    /**
     * String 转时间
     *
     * @param timeStr
     * @return
     */
    public static LocalDateTime parseTime(String timeStr) {
        return LocalDateTime.parse(timeStr, DEFAULT_DATETIME_FORMATTER);
    }

    /**
     * String 转时间
     *
     * @param timeStr
     * @param format  时间格式
     * @return
     */
    public static LocalDateTime parseTime(String timeStr, TimeFormat format) {
        return LocalDateTime.parse(timeStr, format.formatter);
    }

    /**
     * 时间转 String
     *
     * @param time
     * @return
     */
    public static String formatTime(LocalDateTime time) {
        return DEFAULT_DATETIME_FORMATTER.format(time);
    }

    /**
     * 时间转 String
     *
     * @param time
     * @param format 时间格式
     * @return
     */
    public static String formatTime(LocalDateTime time, TimeFormat format) {
        return format.formatter.format(time);
    }

    /**
     * 获取当前时间
     *
     * @return
     */
    public static LocalDateTime getCurrentLocalDatetime() {
        return LocalDateTime.now();
    }
    /**
     * 获取当前时间
     *
     * @return
     */
    public static Date getCurrentDate() {
        return new Date();
    }

    /**
     * 获取当前时间
     *
     * @return
     */
    public static String getCurrentDatetime() {
        return DEFAULT_DATETIME_FORMATTER.format(LocalDateTime.now());
    }

    /**
     * 获取当前时间
     *
     * @param format 时间格式
     * @return
     */
    public static String getCurrentDatetime(TimeFormat format) {
        return format.formatter.format(LocalDateTime.now());
    }

    /**
     * 时间格式
     */
    public enum TimeFormat {

        /**
         * 日期格式
         */
        SHORT_DATE_PATTERN_LINE("yyyy-MM-dd"), SHORT_DATE_PATTERN_SLASH("yyyy/MM/dd"),
        SHORT_DATE_PATTERN_DOUBLE_SLASH("yyyy\\MM\\dd"), SHORT_DATE_PATTERN_NONE("yyyyMMdd"),

        /**
         * 时间格式
         */
        SHORT_TIME_PATTERN_LINE("HH:mm:ss"), SHORT_TIME_PATTERN_WITH_MILSEC_LINE("HH:mm:ss.SSS"),
        SHORT_TIME_PATTERN_NONE("HHmmss"), SHORT_TIME_PATTERN_WITH_MILSEC_NONE("HHmmssSSS"),

        /**
         * 长时间格式
         */
        LONG_DATETIME_PATTERN_LINE("yyyy-MM-dd HH:mm:ss"), LONG_DATETIME_PATTERN_SLASH("yyyy/MM/dd HH:mm:ss"),
        LONG_DATETIME_PATTERN_DOUBLE_SLASH("yyyy\\MM\\dd HH:mm:ss"), LONG_DATETIME_PATTERN_NONE("yyyyMMddHHmmss"),

        /**
         * 长时间格式 带毫秒
         */
        LONG_DATETIME_PATTERN_WITH_MILSEC_LINE("yyyy-MM-dd HH:mm:ss.SSS"),
        LONG_DATETIME_PATTERN_WITH_MILSEC_SLASH("yyyy/MM/dd HH:mm:ss.SSS"),
        LONG_DATETIME_PATTERN_WITH_MILSEC_DOUBLE_SLASH("yyyy\\MM\\dd HH:mm:ss.SSS"),
        LONG_DATETIME_PATTERN_WITH_MILSEC_NONE("yyyyMMddHHmmssSSS");

        private transient DateTimeFormatter formatter;

        TimeFormat(String pattern) {
            formatter = DateTimeFormatter.ofPattern(pattern);
        }

    }

    public static Date LocalDateTimeToDate(LocalDateTime localDateTime) {
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zonedDateTime = localDateTime.atZone(zoneId);
        Date date = Date.from(zonedDateTime.toInstant());
        return date;
    }

    public static LocalDateTime dateToLocalDateTime(Date date) {
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
        return localDateTime;
    }


    public static LocalDateTime addSeconds(LocalDateTime sourcesLocalDateTime, int second) {
        return sourcesLocalDateTime.plusSeconds(second);
    }

    public static LocalDateTime addMinutes(LocalDateTime sourcesLocalDateTime, int minute) {
        return sourcesLocalDateTime.plusMinutes(minute);
    }

    public static LocalDateTime addHours(LocalDateTime sourcesLocalDateTime, int hour) {
        return sourcesLocalDateTime.plusHours(hour);
    }

    public static LocalDateTime addDays(LocalDateTime sourcesLocalDateTime, int day) {
        return sourcesLocalDateTime.plusDays(day);
    }

    public static LocalDateTime addMonths(LocalDateTime sourcesLocalDateTime, int month) {
        return sourcesLocalDateTime.plusMonths(month);
    }

    public static LocalDateTime addYears(LocalDateTime sourcesLocalDateTime, int year) {
        return sourcesLocalDateTime.plusYears(year);
    }

    public static LocalDateTime addWeeks(LocalDateTime sourcesLocalDateTime, int week) {
        return sourcesLocalDateTime.plusWeeks(week);
    }

    public static LocalDateTime minusSeconds(LocalDateTime sourcesLocalDateTime, int second) {
        return sourcesLocalDateTime.minusSeconds(second);
    }

    public static LocalDateTime minusMinutes(LocalDateTime sourcesLocalDateTime, int minute) {
        return sourcesLocalDateTime.minusMinutes(minute);
    }

    public static LocalDateTime minusHours(LocalDateTime sourcesLocalDateTime, int hour) {
        return sourcesLocalDateTime.minusHours(hour);
    }

    public static LocalDateTime minusDays(LocalDateTime sourcesLocalDateTime, int day) {
        return sourcesLocalDateTime.minusDays(day);
    }

    public static LocalDateTime minusMonths(LocalDateTime sourcesLocalDateTime, int month) {
        return sourcesLocalDateTime.minusMonths(month);
    }

    public static LocalDateTime minusYears(LocalDateTime sourcesLocalDateTime, int year) {
        return sourcesLocalDateTime.minusYears(year);
    }

    public static LocalDateTime minusWeeks(LocalDateTime sourcesLocalDateTime, int week) {
        return sourcesLocalDateTime.minusWeeks(week);
    }

    public static void main(String[] args) {
        System.out.println("当前时间：" + TimeUtils.getCurrentDatetime());
        System.out.println("当前时间：" + TimeUtils.getCurrentDatetime(TimeFormat.LONG_DATETIME_PATTERN_DOUBLE_SLASH));
        System.out.println("当前时间：" + TimeUtils.getCurrentDatetime(TimeFormat.LONG_DATETIME_PATTERN_WITH_MILSEC_NONE));

        Date date = TimeUtils.LocalDateTimeToDate(LocalDateTime.now());
        System.out.println(date);

        LocalDateTime localDateTime = TimeUtils.dateToLocalDateTime(new Date());
        System.out.println(localDateTime);

    }
}
